//
//  NearbyTableViewCell.m
//  Findif
//
//  Created by Cicel Chen on 2018/10/22.
//  Copyright © 2018年 Cicel Chen. All rights reserved.
//

#import "NearbyTableViewCell.h"
#import "XHStarRateView.h"
#import <CoreLocation/CoreLocation.h>

@interface NearbyTableViewCell ()

@property (nonatomic, weak)UIImageView * backImageView;
@property (nonatomic, weak)UIImageView * iconImageView;
@property (nonatomic, weak)UILabel * shopLabel;
@property (nonatomic, weak)UILabel * distanceLabel;
@property (nonatomic, weak)XHStarRateView * starView;
@property (nonatomic, weak)UILabel * commentLabel;
@property (nonatomic, weak)UILabel * exploreLabel;
@property (nonatomic, weak)UILabel * localLabel;


@end

@implementation NearbyTableViewCell


- (void)setModel:(ShopListModel *)model{
    
    _model = model;
    
    self.shopLabel.text = model.merchants_name;
    int count = [model.merchants_comments_star_level intValue];
    if (count == 0) {
        
        self.starView.width = 1;
    }else{
        
        self.starView.currentRating = count;
    }
    
    self.commentLabel.text = [NSString stringWithFormat:@"%@条评论",model.merchants_comments_count];
    self.exploreLabel.text = [NSString stringWithFormat:@"店内有%@个折扣活动",model.merchants_discount_goods_count];
    NSArray *array = [model.merchants_img componentsSeparatedByString:@","];
    NSString * img = array.firstObject;
    NSString * usrlStr = [NSString stringWithFormat:@"%@%@",BASEURLSTR,img];
    NSURL * url = [NSURL URLWithString:usrlStr];
    [self.iconImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"组12"]];
    self.localLabel.text = model.merchants_address;
    double lat = [model.merchants_latitude doubleValue];
    double lon = [model.merchants_longitude doubleValue];
    
    double userLat = [DEF_GET_OBJECT(@"lat") doubleValue];
    double userLon = [DEF_GET_OBJECT(@"lon") doubleValue];
    
    double res = [self distanceBetweenOrderBy:lat :userLat :lon :userLon];
    if (res > 1000) {
        
        res = res / 1000;
        self.distanceLabel.text = [NSString stringWithFormat:@"%.2fkm",res];
    }else{
        
        self.distanceLabel.text = [NSString stringWithFormat:@"%.2fm",res];
    }
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setupUI];
    }
    
    return self;
}

- (void)setupUI{
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = [UIColor clearColor];
    
    UIImageView * backImageView = [UIImageView new];
    
    backImageView.image = [UIImage imageNamed:@"RoundedRectangle"];
    self.backImageView = backImageView;
    
    [self.contentView addSubview:self.backImageView];
    
    UIImageView * iconImageView = [UIImageView new];
    
    //    iconImageView.image = IMAGENAME(@"RoundedRectangle");
    iconImageView.backgroundColor = ZRWhiteColor;
    iconImageView.contentMode = UIViewContentModeScaleAspectFit;
    self.iconImageView = iconImageView;
    
    [self.backImageView addSubview:self.iconImageView];
    
    
    UILabel * shopLabel = [UILabel new];
    
    shopLabel.textColor = ZRBlackColor;
    shopLabel.text = @"王者餐厅";
    shopLabel.font = [UIFont boldSystemFontOfSize:15 * SCREENH6];
    shopLabel.textAlignment = NSTextAlignmentLeft;
    
    self.shopLabel = shopLabel;
    
    [self.backImageView addSubview:self.shopLabel];
    
    UILabel * distanceLabel = [UILabel new];
    
    distanceLabel.text = @"170m";
    distanceLabel.textColor = [UIColor lightGrayColor];
    distanceLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    distanceLabel.textAlignment = NSTextAlignmentRight;
    
    self.distanceLabel = distanceLabel;
    
    [self.backImageView addSubview:self.distanceLabel];
    
    
    XHStarRateView *starRateView = [[XHStarRateView alloc] initWithFrame:CGRectMake(105 * SCREENH6, 35 * SCREENH6, 70 * SCREENH6, 10 * SCREENH6)];
    starRateView.isAnimation = YES; // 有动画
    starRateView.rateStyle = XHStarRateViewRateStyeFullStar; //允许不完整星评论
    //    starRateView.delegate = self;
    starRateView.currentRating = 5;
    starRateView.userInteractionEnabled = NO;
    
    self.starView = starRateView;
    [self.backImageView addSubview:self.starView];
    
    
    UILabel * commentLabel = [UILabel new];
    
    commentLabel.text = @"140条评论";
    commentLabel.textColor = [UIColor lightGrayColor];
    commentLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    self.commentLabel = commentLabel;
    
    [self.backImageView addSubview:self.commentLabel];
    
    
    UILabel * exploreLabel = [UILabel new];
    
    exploreLabel.text = @"店内有5个折扣活动";
    exploreLabel.textColor = ZRBlackColor;
    exploreLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    self.exploreLabel = exploreLabel;
    
    [self.backImageView addSubview:self.exploreLabel];
    
    
    UILabel * localLabel = [UILabel new];
    
    localLabel.text = @"成都市武侯大道63号";
    localLabel.textColor = ZRBlackColor;
    localLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    self.localLabel = localLabel;
    
    [self.backImageView addSubview:self.localLabel];
}


- (void)layoutSubviews{
    
    [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.contentView).offset(15 * SCREENH6);
        make.trailing.mas_equalTo(self.contentView).offset(-15 * SCREENH6);
        make.top.bottom.mas_equalTo(self.contentView);
    }];
    
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.backImageView).offset(10 * SCREENH6);
        make.top.mas_equalTo(self.backImageView).offset(15 * SCREENH6);
        make.width.height.mas_equalTo(85 * SCREENH6);
    }];
    
    [self.distanceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.iconImageView);
        make.trailing.mas_equalTo(self.backImageView).offset(-15 * SCREENH6);
    }];
    
    [self.shopLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.iconImageView.mas_trailing).offset(10 * SCREENH6);
        make.width.mas_equalTo(180 * SCREENH6);
        make.centerY.mas_equalTo(self.distanceLabel);
    }];
    
    [self.commentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.starView);
        make.leading.mas_equalTo(self.starView.mas_trailing).offset(5 * SCREENH6);
    }];
    
    [self.exploreLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.starView);
        make.top.mas_equalTo(self.starView.mas_bottom).offset(5 * SCREENH6);
    }];
    
    [self.localLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.exploreLabel);
        make.trailing.mas_equalTo(self.distanceLabel);
        make.top.mas_equalTo(self.exploreLabel.mas_bottom).offset(5 * SCREENH6);
    }];
}

-(double)distanceBetweenOrderBy:(double) lat1 :(double) lat2 :(double) lng1 :(double) lng2{
    
    CLLocation *curLocation = [[CLLocation alloc] initWithLatitude:lat1 longitude:lng1];
    
    CLLocation *otherLocation = [[CLLocation alloc] initWithLatitude:lat2 longitude:lng2];
    
    double  distance  = [curLocation distanceFromLocation:otherLocation];
    
    return  distance;
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
